<!-- 业务步骤配置 -->
<template>
	<div>
		<!-- 搜索表单 -->
		<div>
			<el-form :model="searchFormData" :inline="true">
				<el-row :gutter="10">
					<el-col :span="5">
						<el-form-item label="系统名称" prop="systemId">
							<el-select v-model="searchFormData.systemId" placeholder="请选择系统名称" @change="systemChange">
								<el-option v-for="item in state.systemOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="5">
						<el-form-item label="业务名称" prop="businessCode">
							<el-select v-model="searchFormData.businessCode" placeholder="请选择业务名称">
								<el-option v-for="item in state.businessOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="5">
						<el-form-item label="确认状态" prop="isConfirm">
							<el-select v-model="searchFormData.isConfirm" placeholder="请选择确认状态" clearable>
								<el-option v-for="item in options.statusOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="5">
						<el-form-item label="步骤名称" prop="name">
							<el-input v-model="searchFormData.name" placeholder="请输入步骤名称" clearable class="yd-input" />
						</el-form-item>
					</el-col>
					<el-col :span="4">
						<el-form-item>
							<el-button type="primary" icon="Search" @click="handlerQuery">查 询</el-button>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
		</div>

		<!-- 数据列表 -->
		<YdTable
			:formData="searchFormData"
			:tableConfig="tableConfig"
			:tableData="tableData.data"
			@sizeChangeHandler="handlerQuery"
			@currentChangeHandler="handlerQuery"
			@sortChangeHandler="handlerQuery"
			@selectionChangeHandler="handlerSelectionChange"
		></YdTable>

		<!-- 添加或修改参数配置对话框 -->
		<el-dialog :title="dialog.addOrUpdate.title" v-model="dialog.addOrUpdate.open" width="1000px" center append-to-body>
			<el-form ref="addOrUpdateForm" :model="dialog.addOrUpdate.form" :rules="dialog.addOrUpdate.rule" label-width="120px">
				<el-row :gutter="10">
					<el-col :span="12">
						<el-form-item label="步骤名称" prop="name">
							<el-input v-model="dialog.addOrUpdate.form.name" placeholder="请输入步骤名称" maxlength="200"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="步骤类型" prop="type">
							<el-select v-model="dialog.addOrUpdate.form.type" placeholder="请选择步骤类型" style="width: 100%" @change="changeDisabled">
								<el-option v-for="(option, index) in options.stepTypeOptions" :key="index" :label="option.label" :value="option.value"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="10">
					<el-col :span="12">
						<el-form-item label="服务资源编码" prop="stepCode">
							<el-input v-model="dialog.addOrUpdate.form.stepCode" disabled>
								<template #append>
									<el-button @click="selectResource" icon="Tickets"></el-button>
								</template>
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="步骤顺序" prop="sequence">
							<el-input-number
								v-model="dialog.addOrUpdate.form.sequence"
								placeholder="请输入步骤顺序"
								:max="1000000"
								style="width: 100%"
								:controls="false"
							></el-input-number>
						</el-form-item>
					</el-col>
				</el-row>

				<el-row :gutter="10">
					<el-col :span="12">
						<el-form-item label="是否允许失败" prop="isAllowFail">
							<el-radio-group :disabled="midFail" v-model="dialog.addOrUpdate.form.isAllowFail">
								<el-radio :label="1">是</el-radio>
								<el-radio :label="0">否</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="是否可缺失" prop="isAllowLose">
							<el-radio-group :disabled="lackStep" v-model="dialog.addOrUpdate.form.isAllowLose">
								<el-radio :label="1">是</el-radio>
								<el-radio :label="0">否</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="10">
					<el-col :span="12">
						<el-form-item label="向上关联指标:" prop="getSessionidIndicator">
							<el-select
								v-model="dialog.addOrUpdate.form.getSessionidIndicator"
								placeholder="请选择"
								collapse-tags
								filterable
								:disabled="addDialogForm.indicatorDisabled"
								style="width: 100%"
							>
								<el-option v-for="(dict, index) in options.indicatorOptions" :key="index" :label="dict.label" :value="dict.value"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="向下关联指标:" prop="saveSessionidIndicator">
							<el-select
								v-model="dialog.addOrUpdate.form.saveSessionidIndicator"
								placeholder="请选择"
								collapse-tags
								filterable
								:disabled="addDialogForm.indicatorDisabled"
								style="width: 100%"
							>
								<el-option v-for="(dict, index) in options.indicatorOptions" :key="index" :label="dict.label" :value="dict.value"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<el-button type="primary" @click="handlerSubmit" :loading="dialog.addOrUpdate.btnLoading">确 定</el-button>
				<el-button @click="closeAddOrUpdateDialog">取 消</el-button>
			</template>
		</el-dialog>

		<!-- 选择业务服务资源弹出层 -->
		<select-server-resource ref="selectServerResourceRef"></select-server-resource>

		<!-- 详单组合策略对话框 -->
		<el-dialog :title="combineDialog.combine.title" v-model="combineDialog.combine.open" width="600px" center append-to-body>
			<el-form ref="combineDialogCombineFormRef" :model="combineDialog.combine.form" :rules="combineDialog.combine.rule" label-width="200px">
				<el-form-item label="业务详单组合方式:" prop="mappingType">
					<el-radio-group v-model="combineDialog.combine.form.mappingType">
						<el-radio :label="'unSequence'">专属指标匹配</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="专属指标名称:" prop="stepExclusiveIndex">
					<el-select
						v-model="combineDialog.combine.form.stepExclusiveIndex"
						placeholder="请选择"
						:disabled="addDialogForm.combineIndicatorDisabled"
						@change="handlerRefresh"
					>
						<el-option v-for="(dict, index) in options.combineIndicatorOptions" :key="index" :label="dict.label" :value="dict.value"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="启用状态:" prop="status">
					<el-radio-group v-model="combineDialog.combine.form.status" @change="handlerRefresh">
						<el-radio :label="1">启用</el-radio>
						<el-radio :label="0">禁用</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>

			<br />如下业务因共用该步骤, 将进行同步更新
			<!-- 数据列表 -->
			<el-table :data="combineTableData">
				<el-table-column type="index" width="80" label="序号"></el-table-column>
				<el-table-column prop="name" label="业务名称"></el-table-column>
			</el-table>
			<template #footer>
				<el-button type="primary" @click="handlerCombine" :loading="combineDialog.combine.btnLoading">确 定</el-button>
				<el-button @click="closeCombineDialog">取 消</el-button>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="BusinessStepConfig" lang="tsx">
import { businessProcessOptions } from '@/utils/admin/options';
import {
	query,
	add,
	edit,
	remove,
	confirm,
	cancel,
	getIndicator,
	getStrategy,
	saveStrategy,
	queryBusiness,
	system,
	business,
} from '@/api/admin/businessMonitor/businessStepConfig';
import SelectServerResource from './selectServerResource.vue';
import { getStepInitInfo } from '@/api/admin/netend2netend/netend_sampling_detail';
import { getCurrentInstance, onActivated, onMounted, reactive, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';

const { proxy }: any = getCurrentInstance();
const route = useRoute();
const router = useRouter();

let typeValue: string | number = '';
let systemConfigValue: string = '';
const midFail = ref(true);
const lackStep = ref(true);
const options: any = reactive(businessProcessOptions());
let cacheSearchFormData: any = {};
// 搜索表单数据
let searchFormData = reactive<any>({
	pageSize: 10,
	pageNum: 1,
	// 默认按启动时间降序
	// orderByColumn: 'id',
	isAsc: 'desc',
	...route.query,
});

// 弹出层
let combineDialog = reactive<any>({
	combine: {
		// 打开状态
		open: false,
		// 标题
		title: '详单组合策略配置',
		// 表单数据
		form: {},
		// 表单校验规则
		rule: {},
		// 按钮加载中状态
		btnLoading: false,
	},
});
//新增显示层
let addDialogForm = reactive<any>({
	indicatorDisabled: true,
	combineIndicatorDisabled: true,
});
// 列表配置
let tableConfig = reactive<any>({
	url: query,
	columns: [
		{
			label: '步骤名称',
			prop: 'name',
			showOverFlowToolTip: true,
		},
		{
			label: '步骤类型',
			prop: 'type',
			width: 80,
			showOverFlowToolTip: true,
			formatter: (row: any, val: any) => {
				return options.stepTypeOptionsMap[val];
			},
		},
		{
			label: '步骤编码',
			prop: 'stepCode',
			width: 150,
			render: (row: any, val: any) => {
				return (
					// <router-link to={'/admin/businessSourceManage/detail/' + val} class="link-type">
					// 	<span>{val}</span>
					// </router-link>
					<el-button type="primary" link onClick={stepCodeClick.bind(this, row)}>
						{val}
					</el-button>
				);
			},
		},
		{
			label: '资源匹配串',
			prop: 'identifyStr',
			width: 300,
			showOverFlowToolTip: true,
		},
		{
			label: '确认状态',
			prop: 'isConfirm',
			width: 80,
			formatter: (row: any, val: any) => {
				return options.statusOptionsMap[val];
			},
		},
		{
			label: '端到端关联',
			prop: '',
			operation: [
				{
					text: '配置',
					handler: (scope: any) => jumpEndToEndHandle(scope.row),
					permi: ['business:endToEnd'],
				},
				{
					text: '学习详情',
					// handler: (scope: any) => jumpEndToEndStudyHandle(scope.row),
				},
			],
		},
		{
			label: '步骤顺序',
			prop: 'sequence',
			width: 80,
			showOverFlowToolTip: true,
		},
		{
			label: '是否配置组合策略',
			prop: 'isCombinedStrategy',
			formatter: (row: any, val: any) => {
				return options.isOptionsMap[val];
			},
		},
		{
			label: '详单配置组合策略',
			prop: '',
			operation: [
				{
					text: '详单配置组合策略',
					handler: (scope: any) => detailCombineHandle(scope.row),
					permi: ['business:businessProcess:detail'],
				},
			],
		},
		{
			label: '向上关联指标',
			prop: 'getSessionidIndicatorName',
		},
		{
			label: '向下关联指标',
			prop: 'saveSessionidIndicatorName',
		},
		{
			label: '是否允许失败',
			prop: 'isAllowFail',
			width: 70,
			showOverFlowToolTip: true,
			formatter: (row: any, val: any) => {
				return options.isOptionsMap[val];
			},
		},
		{
			label: '是否可缺失',
			prop: 'isAllowLose',
			width: 70,
			showOverFlowToolTip: true,
			formatter: (row: any, val: any) => {
				return options.isOptionsMap[val];
			},
		},
	],
	// 工具栏
	toolBar: [
		{
			name: '新增',
			icon: 'yd-icon-plus',
			permi: ['business:businessProcess:add'],
			handler: () => handlerAdd(),
		},
		{
			name: '修改',
			icon: 'yd-icon-edit',
			permi: ['business:businessProcess:update'],
			disable: 'single',
			handler: () => handlerEdit(),
		},
		{
			name: '删除',
			icon: 'yd-icon-delete',
			permi: ['business:businessProcess:delete'],
			disable: 'multiple',
			handler: () => handlerDelete(),
		},
		{
			name: '确认',
			icon: 'yd-icon-confirm',
			permi: ['business:businessProcess:confirm'],
			disable: 'check',
			handler: () => handlerConfirm(),
			check: () => checkStatus(0),
		},
		{
			name: '取消',
			icon: 'yd-icon-cancel',
			permi: ['business:businessProcess:cancel'],
			disable: 'check',
			handler: () => handlerCancel(),
			check: () => checkStatus(1),
		},
	],
	// 开启复选框
	enableSelection: true,
	// 开启分页
	pagination: {
		pageNum: 1,
		pageSize: 10,
		total: 0,
	},
	// 加载层
	loading: true,
});
const state = reactive<any>({
	systemOptions: [] as any,
	businessOptions: [] as any,
});
// 列表数据
let tableData = reactive<any>({
	data: [],
	total: 0,
});
// 搜索表单数据
let combineFormData = reactive<any>({
	pageSize: 10,
	pageNum: 1,
	// 默认按启动时间降序
	orderByColumn: 'id',
	isAsc: 'desc',
});
// 列表数据
let combineTableData: any = ref([]);
// 选择的行数据ids
const selectedIds: any = ref(null);
// 选择的行数据
const selected: any = ref(null);
let dialog = reactive<any>({
	addOrUpdate: {
		// 打开状态
		open: false,
		// 标题
		title: '',
		// 表单数据
		form: {},
		// 按钮加载中状态
		btnLoading: false,
		// 表单校验规则
		rule: {
			name: [{ required: true, message: '步骤名称不能为空', trigger: 'blur' }],
			type: [{ required: true, message: '步骤类型必选', trigger: 'change' }],
			stepCode: [{ required: true, message: '步骤业务服务资源编码必选', trigger: 'change' }],
		},
	},
});
let firstRender: boolean = true;

function formInited() {
	if (firstRender) {
		if (route.query.businessCode) {
			searchFormData.businessCode = route.query.businessCode;
		}
		handlerQuery();
	}
}
formInited();
function stepCodeClick(row: any) {
	router.push({
		path: '/management/indicatorManage/proprietaryIndicatorManage/detail',
		query: {
			resourceCode: row.stepCode,
			identifyStr: row.identifyStr,
		},
	});
}
/**
 * 关闭新增或修改弹出层
 */
function closeAddOrUpdateDialog() {
	dialog.addOrUpdate.open = false;
}
/**
 * 选择行数据
 */
function handlerSelectionChange(selects: any) {
	selectedIds.value = selects.map((item: any) => item.id);
	selected.value = selects;
}
/**
 * 修改el-radio里的disabled值
 */
function changeDisabled(value: number | string) {
	if (value == 1) {
		midFail.value = true;
		lackStep.value = false;
	} else if (value == 2) {
		midFail.value = false;
		lackStep.value = false;
	} else if (value == 3) {
		midFail.value = true;
		lackStep.value = true;
	}
	if (typeValue === 1 && systemConfigValue) {
		dialog.addOrUpdate.form.isAllowFail = 0;
		dialog.addOrUpdate.form.isAllowLose = 0;
	}
}
/**
 * 判断是否能取消与确认
 */
function checkStatus(status: any) {
	let flag = false;
	if (selected.value !== undefined && selected.value !== null && selected.value.length > 0) {
		selected.value.forEach((item: any) => {
			if (item.isConfirm == status) {
				flag = true;
			}
		});
	}
	return flag;
}
function handlerRefresh() {
	proxy.$forceUpdate();
}
/**
 * 搜索
 */
function handlerQuery() {
	tableConfig.loading = true;
	cacheSearchFormData = searchFormData;
	if (!searchFormData.businessCode) {
		proxy.$modal.msgWarning('当前系统未配置业务，请检查并配置');
		tableConfig.loading = false;
		return;
	}
	query(searchFormData)
		.then((res: ApiResponse | any) => {
			tableConfig.loading = false;
			if (res.code !== 0) {
				return;
			}
			if (res.result !== null) {
				tableData.data = res.data.records;
				tableConfig.pagination.total = res.data.total;
				typeValue = tableData.data[0]?.type || '';
				systemConfigValue = tableData.data[0]?.systemConfigValue || '';
			}
		})
		.catch((e: any) => {
			tableConfig.loading = false;
		});
}
// 表单提交
function handlerSubmit() {
	if (dialog.addOrUpdate.title === '新增') {
		handlerAdd();
	} else {
		handlerEdit();
	}
}
/**
 * 新增
 */
function handlerAdd() {
	// 判断窗口是否打开
	if (!dialog.addOrUpdate.open) {
		if (cacheSearchFormData.businessCode === undefined) {
			proxy.$modal.msgWarning('当前系统未配置业务，请检查并配置');
			return;
		}
		query(searchFormData).then((res: ApiResponse | any) => {
			tableConfig.loading = false;
			if (res.code !== 0) {
				return;
			}
			if (res.data !== null) {
				tableData.data = res.data.records;
				tableConfig.pagination.total = res.data.total;
				typeValue = tableData.data[0]?.type;
				systemConfigValue = tableData.data[0]?.systemConfigValue;
			}
		});
		// 表单重置
		dialog.addOrUpdate.form = {};
		//设置form中的midFail和lackStep的默认值
		var obj = { isAllowFail: 0, isAllowLose: 0 };
		dialog.addOrUpdate.form = obj;
		//设置data中midFail和lackStep的默认值
		midFail.value = true;
		lackStep.value = true;
		// 设置系统编码
		dialog.addOrUpdate.form.systemId = cacheSearchFormData.systemId;
		dialog.addOrUpdate.form.businessCode = cacheSearchFormData.businessCode;
		// proxy.resetForm('addOrUpdateForm');
		// 标题
		dialog.addOrUpdate.title = '新增';
		// 打开窗口
		dialog.addOrUpdate.open = true;
		return;
	}
	// 校验参数
	proxy.$refs['addOrUpdateForm'].validate((valid: boolean | any) => {
		if (!valid) {
			return;
		}
		// 调用接口
		dialog.addOrUpdate.btnLoading = true;
		add(dialog.addOrUpdate.form)
			.then((res: ApiResponse | any) => {
				dialog.addOrUpdate.btnLoading = false;
				if (res.code !== 0) {
					proxy.$modal.msgError(res.msg);
					return;
				}
				// 关闭弹出层
				closeAddOrUpdateDialog();
				// 刷新列表
				searchFormData.pageNum = 1;
				handlerQuery();
				// 提示
				proxy.$modal.msgSuccess('新增成功！');
			})
			.catch((reason) => {
				dialog.addOrUpdate.btnLoading = false;
			});
	});
}
/**
 * 修改
 */
function handlerEdit() {
	// 判断窗口是否打开
	if (!dialog.addOrUpdate.open) {
		// 数据回显，数据需要拷贝一下
		const d = JSON.parse(JSON.stringify(selected.value[0]));
		dialog.addOrUpdate.form = d;
		if (selected.value[0].type == 1) {
			midFail.value = true;
			lackStep.value = false;
		} else if (selected.value[0].type == 2) {
			midFail.value = false;
			lackStep.value = false;
		} else if (selected.value[0].type == 3) {
			midFail.value = true;
			lackStep.value = true;
		}
		// 设置系统编码
		dialog.addOrUpdate.form.systemId = cacheSearchFormData.systemId;
		dialog.addOrUpdate.form.stepCode = selected.value[0].stepCode;

		// 标题
		dialog.addOrUpdate.title = '修改';
		// 打开窗口
		dialog.addOrUpdate.open = true;
		//查询专属指标
		getIndicator({
			resourceCode: dialog.addOrUpdate.form.stepCode,
			pageSize: '-1',
			pageNum: '-1',
		})
			.then((res: ApiResponse | any) => {
				const data = res.data.records;
				options.indicatorOptions = [];
				if (data != null && data.length > 0) {
					addDialogForm.indicatorDisabled = false;
					options.indicatorOptions.push({
						label: '请选择',
					});
					for (var i = 0; i < data.length; i++) {
						let temp: any = {};
						temp.value = '' + data[i].id;
						temp.label = data[i].name;
						options.indicatorOptions.push(temp);
					}
					proxy.$forceUpdate();
				} else {
					dialog.addOrUpdate.form.getSessionidIndicator = null;
					dialog.addOrUpdate.form.saveSessionidIndicator = null;
					addDialogForm.indicatorDisabled = true;
				}
			})
			.catch((error) => {});
		return;
	}
	// 校验参数
	proxy.$refs['addOrUpdateForm'].validate((valid: any) => {
		if (!valid) {
			return;
		}
		// 调用接口
		dialog.addOrUpdate.btnDisabled = true;
		edit(dialog.addOrUpdate.form).then((res: ApiResponse | any) => {
			dialog.addOrUpdate.btnDisabled = false;
			if (res.code !== 0) {
				proxy.$modal.msgError(res.msg);
				return;
			}
			// 关闭弹出层
			closeAddOrUpdateDialog();
			// 刷新列表
			searchFormData.pageNum = 1;
			handlerQuery();
			// 提示
			proxy.$modal.msgSuccess('修改成功！');
		});
	});
}
/**
 * 删除
 */
function handlerDelete() {
	proxy.$modal
		.confirm('是否确认删除所选项?', '警告', {
			confirmButtonText: '确定',
			cancelButtonText: '取消',
			type: 'warning',
		})
		.then(() => {
			// 调用接口
			return remove(selectedIds.value);
		})
		.then(() => {
			// 刷新列表 计算是否要查询上一页的数据
			if (tableData.total && searchFormData.pageSize) {
				// 删除后的总条数
				let totalCount = tableData.total - selectedIds.length;
				// 计算删除后的总页数
				let totalPage = Math.ceil(totalCount / searchFormData.pageSize);
				// 如果删除后的总页数小于1, 置为1
				totalPage = totalPage < 1 ? 1 : totalPage;
				// 判断删除前的页数是否大于删除后的总页数, 计算当前页应该是多少赋值给查询表单
				searchFormData.pageNum = searchFormData.pageNum > totalPage ? totalPage : searchFormData.pageNum;
			} else {
				searchFormData.pageNum = 1;
			}
			handlerQuery();
			// 提示
			proxy.$modal.msgSuccess('删除成功！');
		});
}
/**
 * 确认
 */
function handlerConfirm() {
	proxy.$modal
		.confirm('是否确认所选步骤?', '警告', {
			confirmButtonText: '确定',
			cancelButtonText: '取消',
			type: 'warning',
		})
		.then(() => {
			// 调用接口
			return confirm(selected.value.map((item: any) => item.id));
		})
		.then(() => {
			// 刷新列表
			searchFormData.pageNum = 1;
			handlerQuery();
			// 提示
			proxy.$modal.msgSuccess('确认成功！');
		})
		.catch(function () {});
}
/**
 * 取消确认
 */
function handlerCancel() {
	proxy.$modal
		.confirm('是否将所选项改为未确认状态?', '警告', {
			confirmButtonText: '确定',
			cancelButtonText: '取消',
			type: 'warning',
		})
		.then(() => {
			// 调用接口
			return cancel(selected.value.map((item: any) => item.id));
		})
		.then(() => {
			// 刷新列表
			searchFormData.pageNum = 1;
			handlerQuery();
			// 提示
			proxy.$modal.msgSuccess('取消确认成功！');
		})
		.catch(function () {});
}
// 选择业务服务资源
function selectResource() {
	proxy.$refs.selectServerResourceRef.openDialog(() => {
		const selected = proxy.$refs.selectServerResourceRef.getSelectServerResource();
		dialog.addOrUpdate.form.stepCode = selected.resourceCode;
		dialog.addOrUpdate.form.identifyStr = selected.identifyStr;
		// 联动查询业务服务资源关联信息列表
		//查询专属指标
		getIndicator({
			resourceCode: selected.resourceCode,
		})
			.then((res: ApiResponse | any) => {
				const data = res.data.records;
				options.indicatorOptions = [];
				if (data != null && data.length > 0) {
					addDialogForm.indicatorDisabled = false;
					options.indicatorOptions.push({
						label: '请选择',
					});
					for (var i = 0; i < data.length; i++) {
						let temp: any = {};
						temp.value = data[i].name;
						temp.label = data[i].coding;
						options.indicatorOptions.push(temp);
					}
				} else {
					dialog.addOrUpdate.form.getSessionidIndicator = null;
					dialog.addOrUpdate.form.saveSessionidIndicator = null;
					addDialogForm.indicatorDisabled = true;
				}
			})
			.catch(function (error) {});
	});
}
//点击跳转到端到端展示页面
function jumpEndToEndHandle(row: any) {
	router.push({
		path: '/management/stepManage/endToEndConfig/index',
		query: {
			systemId: cacheSearchFormData.systemId,
			businessCode: cacheSearchFormData.businessCode,
			stepCode: row.stepCode,
		},
	});
}
//点击跳转到端到端学习详情页面
function jumpEndToEndStudyHandle(row: any) {
	// 查询系统和业务信息
	getStepInitInfo({
		systemId: cacheSearchFormData.systemId,
		businessCode: cacheSearchFormData.businessCode,
		stepCode: row.stepCode,
	}).then((res: ApiResponse | any) => {
		const data = res.result;
		if (res.code === 200) {
			router.push({
				path: '/monitorConfig/businessConfig/segmentStudy/netendSampleDetail',
				query: {
					systemId: data.systemId,
					stepBusinessCode: data.businessCode,
					businessCode: data.businessCode,
					systemCode: data.systemId,
					resourceCoding: row.stepCode,
					identifyStr: row.identifyStr,
				},
			});
		} else {
			proxy.$modal.msgError(res.msg);
		}
	});
}
function detailCombineHandle(row: any) {
	combineDialog.combine.form = {};
	combineDialog.combine.open = true;
	combineDialog.combine.form.stepId = row.id;
	combineDialog.combine.form.stepCode = row.stepCode;
	combineDialog.combine.form.systemId = row.systemId;
	combineDialog.combine.form.businessCode = cacheSearchFormData.businessCode;
	combineDialog.combine.form.mappingType = 'unSequence';
	//查询关联策略
	getStrategy(combineDialog.combine.form).then((res: ApiResponse | any) => {
		if (res !== null && res.data !== null) {
			const data = res.data;
			combineDialog.combine.form.id = data.id;
			combineDialog.combine.form.stepExclusiveIndex = data.stepExclusiveIndex;
			combineDialog.combine.form.status = data.status;
		} else {
			combineDialog.combine.form.status = 0;
		}
		proxy.$forceUpdate();
	});
	//查询专属指标
	getIndicator({
		resourceCoding: row.stepCode,
	}).then((res: any) => {
		const data = res.data.records;
		options.combineIndicatorOptions = [];
		if (data != null && data.length > 0) {
			addDialogForm.combineIndicatorDisabled = false;
			for (var i = 0; i < data.length; i++) {
				let temp: any = {};
				temp.value = data[i].name;
				temp.label = data[i].coding;
				options.combineIndicatorOptions.push(temp);
			}
		} else {
			combineDialog.combine.form.stepExclusiveIndex = null;
			addDialogForm.combineIndicatorDisabled = true;
		}
		proxy.$forceUpdate();
	});
	let obj = {
		stepId: combineDialog.combine.form.stepId,
		stepCode: row.stepCode,
		systemId: searchFormData.systemId,
	};

	//查询共用步骤业务
	queryBusiness(obj).then((res: ApiResponse | any) => {
		combineTableData.value = res.data.records;
	});
}
function closeCombineDialog() {
	combineDialog.combine.open = false;
}
function handlerCombine() {
	if (
		combineDialog.combine.form.stepExclusiveIndex === undefined ||
		combineDialog.combine.form.stepExclusiveIndex === null ||
		combineDialog.combine.form.stepExclusiveIndex === ''
	) {
		proxy.$modal.msgError('请选择专属指标');
		return;
	}
	combineDialog.combine.btnLoading = true;
	// combineDialog.combine.form.confStepJoinMapping
	combineDialog.combine.form.systemId = searchFormData.systemId;
	saveStrategy(combineDialog.combine.form).then((res: ApiResponse | any) => {
		combineDialog.combine.btnLoading = false;
		if (res.code !== 0) {
			proxy.$modal.msgError(res.msg);
			return;
		}
		// 关闭弹出层
		closeCombineDialog();
		// 刷新列表
		searchFormData.pageNum = 1;
		handlerQuery();
		// 提示
		proxy.$modal.msgSuccess('设置关联策略成功！');
	});
}
function systemChange(val: number | string) {
	searchBusinssFn(val);
}

/**
 * 查询系统选项
 */
function searchSystemFn(): void {
	system().then((res: ApiResponse | any) => {
		state.systemOptions = res.data;
		if (route.query.systemId) {
			searchFormData.systemId = route.query.systemId;
		} else {
			searchFormData.systemId = state.systemOptions[0].value;
		}
		searchBusinssFn(searchFormData.systemId);
	});
}

/**
 * 查询系统选项
 */
function searchBusinssFn(systemId: string | number): void {
	business(systemId).then((res: ApiResponse | any) => {
		state.businessOptions = res.data;
		if (!firstRender) {
			searchFormData.businessCode = state.businessOptions.length > 0 ? state.businessOptions[0].value : '';
		}
		firstRender = false;
		handlerQuery();
	});
}
onMounted(async () => {
	await searchSystemFn();
});
</script>
<style lang="scss" scoped>
.el-form-item,
.el-select,
.custom-select-container {
	width: 100%;
}
</style>
